﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using ChiDao.Business.Service.Impl;
using ChiDao.Site.Models;
using Ext.Net.MVC;
using Ext.Net;
using ChiDao.Business.Models.FlowProcess;
using System.Xml;
using System.Web.Script.Serialization;
using ChiDao.Business.Service;
using ChiDao.Component.Utils;


namespace ChiDao.Site.Web.Controllers
{
    [Export]
    public class ProcessNodeController : BaseController
    {
        [Import]
        public IProcessNodeService ProcessNodeService { get; set; }
        [Import]
        public IBaklogTaskService BaklogTaskService { get; set; }
        [Import]
        public IHolidayDocumentService HolidayDocumentService { get; set; }

        public ActionResult CirculationHolidayDocument(string processID, string handleState)
        {
            CirculationProcessNodeView circulationProcessNodeView = new CirculationProcessNodeView();
            circulationProcessNodeView.ID = processID;
            circulationProcessNodeView.HandleState = handleState;
            string currentProcessNodeCode = string.Empty;
            if (handleState == "0")
            {
                circulationProcessNodeView.lstProcessNode = ProcessNodeService.QuerySpecProcessNode("HolidayDocument", true);
                currentProcessNodeCode = circulationProcessNodeView.lstProcessNode[0].PreviousNodeCode;
            }
            else
            {
                string nextProcessNodeCode = BaklogTaskService.GetNextProcessNodeCode(User.Identity.Name, processID,ref currentProcessNodeCode);
                circulationProcessNodeView.lstProcessNode = ProcessNodeService.QuerySpecProcessNode("HolidayDocument", false, nextProcessNodeCode);
            }
            circulationProcessNodeView.CurrentProcessNodeCode = currentProcessNodeCode;
            return View(circulationProcessNodeView);
        }

        [DirectMethod]
        public ActionResult SaveCirculationHolidayDocumentData(string processID, string handleState, string currentProcessNodeCode, string[] circulationDataList, string approvalOpinion)
        {
            //获取当前请假信息
            HolidayDocumentView holidayDocumentView = HolidayDocumentService.GetHolidayDocumentByID(processID);
            //获取当前节点信息
            List<ProcessNodeView> currentProcessNodeInfo = ProcessNodeService.QuerySpecProcessNode("HolidayDocument", false, currentProcessNodeCode);
            if (holidayDocumentView != null && currentProcessNodeInfo != null && currentProcessNodeInfo.Count == 1 && circulationDataList.Length == 1)
            {
                string circulationData = "{\"circulationData\":" + circulationDataList[0] + "}";
                XmlNode circulationDataNode = JSON.DeserializeXmlNode(circulationData);
                string nodeCode = circulationDataNode.SelectNodes("circulationData/NodeCode")[0].InnerText;
                string nodeName = circulationDataNode.SelectNodes("circulationData/NodeName")[0].InnerText;
                string nextNodeCode = circulationDataNode.SelectNodes("circulationData/NextNodeCode")[0].InnerText;
                string processPersonStr = circulationDataNode.SelectNodes("circulationData/DefaultProcessPerson")[0].InnerText;

                //更新请假单状态
                if (handleState == "0")
                {
                    HolidayDocumentService.UpdateHolidayDocument(Guid.Parse(processID), DateTime.Now, null, "1");
                }
                if (nodeCode == "End")
                {
                    HolidayDocumentService.UpdateHolidayDocument(Guid.Parse(processID), DateTime.Now, null, "2");
                }

                //更新待办
                List<BaklogTaskView> originalList = BaklogTaskService.QueryBaklogTask(User.Identity.Name, processID);
                if (!string.IsNullOrEmpty(currentProcessNodeInfo[0].ProcessPersonCondition) && currentProcessNodeInfo[0].ProcessPersonCondition.ToString() == "and")
                {
                    List<BaklogTaskView> updateList = new List<BaklogTaskView>();
                    BaklogTaskView updateInfo = originalList.Find(b => b.CurrentProcessPerson == User.Identity.Name);
                    updateInfo.ApprovalOpinion = approvalOpinion;
                    updateInfo.HandleState = "1";
                    updateInfo.ProcessTime = DateTime.Now;
                    updateList.Add(updateInfo);
                    BaklogTaskService.UpdateBaklogTask(updateList);
                }
                else
                {
                    foreach (BaklogTaskView update in originalList)
                    {
                        if (update.CurrentProcessPerson == User.Identity.Name)
                        {
                            update.ApprovalOpinion = approvalOpinion;
                        }
                        update.HandleState = "1";
                        update.ProcessTime = DateTime.Now;
                    }
                    BaklogTaskService.UpdateBaklogTask(originalList);
                }

                //添加待办
                originalList = BaklogTaskService.QueryBaklogTask(User.Identity.Name, processID);
                if (nodeCode != "End" && originalList.Count == 0)
                {
                    List<BaklogTaskView> addList = new List<BaklogTaskView>();
                    string[] processPersonArr = processPersonStr.Split(',');
                    foreach (string processPerson in processPersonArr)
                    {
                        BaklogTaskView add = new BaklogTaskView();
                        add.ID = GuidHelper.NewGuid();
                        add.ProcessID = processID;
                        add.TaskName = "请假单";
                        add.RequestPerson = holidayDocumentView.RequestPerson;
                        add.CurrentProcessNodeCode = nodeCode;
                        add.CurrentProcessNodeName = nodeName;
                        add.NextProcessNodeCode = nextNodeCode;
                        add.HandleState = "0";
                        add.CurrentProcessPerson = processPerson;
                        add.ProcessTime = DateTime.Now;
                        addList.Add(add);
                    }
                    BaklogTaskService.AddBaklogTask(addList);
                }
                X.Msg.Confirm("提示", "流转成功!", new MessageBoxButtonsConfig
                {
                    Ok = new MessageBoxButtonConfig { Text = "Ok" }
                }).Show();
            }
            else
            {
                X.Msg.Confirm("提示", "流程数据不对，流转不成功!", new MessageBoxButtonsConfig
                {
                    Ok = new MessageBoxButtonConfig { Text = "Ok" }
                }).Show();
            }
            return this.Direct();
        }
    }
}